package utils

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"

	"github.com/tidwall/gjson"
)

//经纬度转化地址
func Longitude_and_latitude(location string) (addres string, err error) {
	// 替换成你在高德地图申请的API Key
	apiKey := "478788ad10caae843b4b8ad0b38ac1cf"

	

	// 构建请求参数
	parameters := url.Values{}
	parameters.Set("key", apiKey)
	parameters.Set("location", location)

	// 构建请求URL
	requestURL := "https://restapi.amap.com/v3/geocode/regeo?" + parameters.Encode()

	// 发送GET请求
	response, err := http.Get(requestURL)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return "",err
	}
	defer response.Body.Close()

	// 读取响应内容
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Println("Error reading response body:", err)
		return "",err

	}

	// 使用 gjson 提取省和区信息
	province := gjson.Get(string(body), "regeocode.addressComponent.province").String()
	district := gjson.Get(string(body), "regeocode.addressComponent.district").String()
	addres=province+"-"+district
	return addres,nil
}